import { observable, action } from 'mobx';
import { WeaLocaleProvider } from 'ecCom';
import { WeaForm } from 'comsMobx';
const getLabel = WeaLocaleProvider.getLabel;
import * as Apis from "../apis/task"
import { message } from 'antd';
import {toJS} from "mobx"

/**
 * @author yxd 2019-09-30
 * 任务批量编辑
 */
export class TaskBatchEditDialogStore{
    @observable visible = false;
    @observable bmOptions = [];
    @observable bmOptionValue = "";
    @observable bmFieldValue = "";
    @observable bmform = new WeaForm();
    @observable bmCondition = []
    @observable selectField = {
        fieldname : "",
        showname : "",
    };
    @observable bmDatas = [];
    @observable bmSelectedDatas = [];
    @observable bmSelectedRowKeys = [];
    @observable bmSelectComs = [];
    @observable prjid = '';
    
    @action
    setVisible = (v)=>{
        const that = this;
        if(v){
            that.visible = v;
            Apis.getModifySelect({prjid:this.prjid}).then(data => {
               that.bmOptions = toJS(data.options);
               that.fieldChange("","");
               that.resetBmOptions();
            });
            this.bmDatas = [];
        }else{
            that.visible = v;
            that.bmOptions = []
            that.resetBmOptions();
        }
    }

    @action
    appendBmData = () => {
        let formDatas = this.bmform.getFormDatas();

        let showname = "";
        let fieldname = "";
        let valuespan = "";
        let value = "";

        let bmDatas = this.bmDatas;
        if(this.selectField.fieldname !== ""){
            const _fieldname = this.selectField.fieldname;
            showname = this.selectField.showname;
            fieldname = _fieldname;
            if(typeof(formDatas[_fieldname].valueObj) !== "undefined" || typeof(formDatas[_fieldname].valueSpan) !== "undefined"){
                valuespan = formDatas[_fieldname].valueSpan;
                value = formDatas[_fieldname].value;
            }else{
                valuespan = formDatas[_fieldname].value;
                value = formDatas[_fieldname].value;
            }
            bmDatas.push({
                id : Math.ceil(Math.random()*10000),
                showname : showname,
                fieldname : fieldname,
                valuespan : valuespan,
                value : value
            });
        }
        this.bmDatas = bmDatas;
        this.resetBmOptions();
    }

    @action
    resetBmOptions = () => {
        let options = toJS(this.bmOptions);
        let selectedDatas = toJS(this.bmDatas);
        let new_options = [];
        options.map((option)=>{
            let key = option.key;
            let disabled = false;
            let selected = false;
            selectedDatas.map((data)=>{
                if(data.fieldname == key){
                    disabled = true;
                }
            });
            if(option.key == ""){
                selected = true;
            }
            new_options.push({
                ...option,
                disabled : disabled,
                selected : selected
            });
        });
        this.bmOptionValue = Math.ceil(Math.random()*10000);
        this.bmOptions = new_options;
        this.bmform = new WeaForm();
    }

    @action
    onBmRowSelect = (selectedRowKeys,selectComs) => {
        this.bmSelectedRowKeys = selectedRowKeys;
        this.bmSelectComs = selectComs;
    }

    @action 
    onBmChange = (datas) => {
        this.bmDatas = datas;
        this.resetBmOptions();
    }

    @action 
    fieldChange = (fieldname , showname, prjid=0) => {
        this.selectField = {
            fieldname : fieldname,
            showname : showname,
        }
        this.bmOptionValue = fieldname;
        Apis.getModifyFields({ fieldname:fieldname,type:'prjtsk',viewattr:2,prjid:prjid }).then(data => {
            this.bmform = new WeaForm();
            this.bmCondition = data.fieldinfo;
            this.bmform.initFormFields(this.bmCondition);
        });
    }

    @action
    submitBatchModify = (selectedRowKeys) => {
        if(this.bmDatas.length == 0){
            message.error(getLabel('500403','请选择需要更改的数据!') );
            return;
        }
        Apis.submitBatchModify({ datas : JSON.stringify(this.bmDatas),ids : selectedRowKeys}).then(data => {
            if(data.success){
                message.success(data.msg);
                this.setVisible(false);
                window._table.reLoad();
            }else{
                message.error(data.msg);
            }
        });
    }

    setPrjid = (v) =>{
        this.prjid = v;
    }

}

export default new TaskBatchEditDialogStore